/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gameai.tickettoride.core;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Christopher
 */
public class DestDeck {
    
    public final int DECK_SIZE = 30;
    private ArrayList<DestCard> cards;

    public DestDeck() {
        cards = new ArrayList<DestCard>();
        //Add cards
        for (int c=0; c <= 30; c++) {
            cards.add(new DestCard(c));
        }
        
        shuffle();
    }
    
    /**
     * Draws 3 cards from destination deck
     * 
     * @return Array of 3 DestCard(s)
     */
    public DestCard[] draw() {
        
        DestCard[] offered = new DestCard[3];
        
        for (int i=0; i < 3; i++) {
            offered[i] = cards.remove(i);
        }
        
        return offered;
    }

    /**
     * Place the non-taken cards on bottom of deck
     */
    protected void restack(DestCard[] cds) {
        for(DestCard c : cds){
            cards.add(c);
        }
    }
    
    /**
     * Shuffles the deck.
     */
    public void shuffle(){
        int index1, index2;
        Random generator = new Random();
        
        DestCard temp;
        
        for (int i=0; i < cards.size(); i++) {
            index1 = generator.nextInt(cards.size()-1);
            index2 = generator.nextInt(cards.size()-1);
            
            temp = cards.get(index2);
            cards.set(index2 , cards.get(index1));
            cards.set(index1, temp );
        }
    }
    
    /**
     * Check if deck is empty
     * @return true if DestDeck is empty.
     */
    public boolean isEmpty() {
        return cards.isEmpty();
    }
    
  public double mean(){
    int n = cards.size(); 
    if ( n < 2 ){ 
      return Double.NaN; 
    } 
    double avg = cards.get(0).getValue(); 
    double sum = 0; 
    for ( int i = 1; i < n; i++ ){ 
      avg += cards.get(i).getValue();
    } 
    
    return avg/n; 
  }

  public double standardDeviation() { 
    int n = cards.size(); 
    if ( n < 2 ){ 
      return Double.NaN; 
    } 
    double avg = cards.get(0).getValue(); 
    double sum = 0; 
    for ( int i = 1; i < n; i++ ){ 
      double newavg = avg + ( cards.get(i).getValue() - avg ) / ( i + 1 ); 
      sum += ( cards.get(i).getValue() - avg ) * ( cards.get(i).getValue() -newavg ) ; 
      avg = newavg; 
    } 
    return Math.sqrt( sum / n ); 
  }
}
